<template>
    <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="90" label-position="right">
        <FormItem label="分类" prop="type_id">
            <Select v-model="formValidate.type_id">
                <Option v-for="item in supplierTypes" :value="item.id" :key="item.id">{{ item.title }}</Option>
            </Select>
        </FormItem>
        <FormItem label="供应商名称" prop="supplier_name">
            <Input type="text" v-model="formValidate.supplier_name"></Input>
        </FormItem>
        <FormItem label="联系人">
            <Input type="text" v-model="formValidate.linkman"></Input>
        </FormItem>
        <FormItem label="联系电话">
            <Input type="text" v-model="formValidate.linkphone"></Input>
        </FormItem>
        <FormItem label="开户名称">
            <Input type="text" v-model="formValidate.user_name"></Input>
        </FormItem>
        <FormItem label="开户银行">
            <Input type="text" v-model="formValidate.bank_name"></Input>
        </FormItem>
        <FormItem label="银行帐号">
            <Input type="text" v-model="formValidate.bank_number"></Input>
        </FormItem>
        <FormItem label="地址">
            <Input type="text" v-model="formValidate.address"></Input>
        </FormItem>        
        <FormItem label="是否提供发票">            
            <RadioGroup v-model="formValidate.is_invoice">
                <Radio :label="0">否</Radio>
                <Radio :label="1">是</Radio>               
            </RadioGroup>             
        </FormItem>        
        <FormItem label="结算方式">
            <RadioGroup v-model="formValidate.settlement">
                <Radio :label="1">月结</Radio>
                <Radio :label="2">半月结</Radio>
                <Radio :label="3">现结</Radio>
                <Radio :label="4">预存款</Radio>
            </RadioGroup>
        </FormItem>        
        <FormItem>
            <Button type="primary" @click="handleSubmit('formValidate')">确定</Button>
        </FormItem>
    </Form>
</template>
<script>

export default {
    name: 'supplierinfo',
    props: ['formDate'],
    data(){
        return {  
            formValidate: this.formDate,
            supplierTypes: '',            
            ruleValidate:{
                supplier_name:[
                    { required: true, message: '请输入供应商名称', trigger: 'blur' }
                ],
                type_id:[
                    { type:'number',required: true, message: '请输入选择分类', trigger: 'blur' }
                ]
            }       
        }
    },
    created(){        
        this.$axios({
            mothod: 'post',
            url: 'http://localhost/public/api/supplier_type',
            params: ''
        }).then((response)=> { 
            this.supplierTypes = response.data;            
        })
        .catch(function (error) {
            console.log(error);
        }); 
    },
    methods: { 
        addSupplier(){
            this.$axios({
                mothod: 'post',
                url: 'http://localhost/public/api/supplier/create',
                params: this.formValidate
            }).then((response)=> { 
                this.$Message.success('录入成功!'); 
            })
            .catch(function (error) {
                this.$Message.error('录入失败!');
            });   
        },
        editSupplier(){
            this.$axios({
                mothod: 'post',
                url: 'http://localhost/public/api/supplier/update',
                params: this.formValidate
            }).then((response)=> { 
                this.$Message.success('编辑成功!'); 
            })
            .catch(function (error) {
                this.$Message.error('编辑失败!');
            });   
        },                 
        handleSubmit(name){                 
            this.$refs[name].validate((valid) => {                   
                if (valid) {                                 
                    if(this.formValidate.id != ""){
                        this.editSupplier(); 
                    }else{
                        this.addSupplier();                                 
                    }  
                    this.$emit('updateSupplierList');    
                    this.$emit('closeDialog');              
                } else {
                    this.$Message.error('提交失败!');
                }
            })
        }
    }
}
</script>
